<template>
  <div class="imageUpload" :class="{ disabled: isLimit }">
    <el-upload
      action="#"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :file-list="fileList"
      :limit="limit"
      :on-exceed="onExceed"
      :http-request="httpRequest"
      :on-change="onChange"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" title="图片预览">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import cos from '@/utils/cos'
export default {
  props: {
    limit: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      fileList: []
    }
  },

  computed: {
    isLimit() {
      return this.fileList.length >= this.limit
    },
    isAllUpload() {
      return this.fileList.every((item) => item.status === 'success')
    }
  },
  methods: {
    httpRequest({ file }) {
      if (!file) return
      const curFileObj = this.fileList.find((item) => item.uid === file.uid)
      curFileObj.status = 'uploading'
      cos.putObject(
        {
          Bucket: 'hrssass-151-1315229280' /* 存储桶 */,
          Region: 'ap-beijing' /* 存储桶所在地域，必须字段 */,
          Key: +new Date() + file.name /* 文件名 */,
          Body: file, // 上传文件对象
          onProgress: function(progressData) {
            // 获取实时进度
            // console.log(JSON.stringify(progressData))
            curFileObj.percentage = progressData.percent * 100
          }
        },
        function(err, data) {
          // console.log(err || data)
          if (err) {
            this.$message.error('图片上传失败')
            return
          }
          if (data.statusCode === 200) {
            curFileObj.status = 'success'
            curFileObj.url = '//' + data.Location
          }
        }
      )
    },
    onChange(file, fileList) {
      // 第一种
      this.fileList = [...fileList]
      // 第二种
      // this.fileList = this.fileList.filter((item) => item.uid !== file.uid)
    },
    onExceed() {
      this.$message.warning('您上传的图片数量已经达到最大值')
    },
    // 删除图片
    handleRemove(file, fileList) {
      // console.log(file, fileList)
      // this.fileList = [...fileList]
      this.fileList = this.fileList.filter((item) => item.uid !== file.uid)
    },
    // 预览图片放大
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    beforeUpload(file) {
      // 1. 限制文件类型 file.type
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      if (!types.includes(file.type)) {
        this.$message.error('上传的图片格式, 必须是jpg, gif, bmp, png的格式!')
        return false
      }
      // 2. 限制文件大小 file.size
      if (file.size / 1024 / 1024 >= 5) {
        this.$message.error('上传头像过大, 超过了5M, 必须5M以内')
        return false
      }
      return true
    }
  }
}
</script>

<style lang="scss" scoped>
.disabled {
  ::v-deep {
    .el-upload--picture-card {
      display: none;
    }
  }
}
</style>